package com.atguigu.system.service.impl;

import com.atguigu.model.system.SysRole;
import com.atguigu.model.system.SysUserRole;
import com.atguigu.model.vo.AssginRoleVo;
import com.atguigu.model.vo.SysRoleQueryVo;
import com.atguigu.system.mapper.SysRoleMapper;
import com.atguigu.system.mapper.SysUserRoleMapper;
import com.atguigu.system.service.SysRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public IPage<SysRole> findPage(Page<SysRole> page, SysRoleQueryVo roleQueryVo) {
        return sysRoleMapper.findPage(page,roleQueryVo);
    }

    @Override
    public Map<String, Object> getRolesByUserId(Long userId) {
        //获取所有的角色
        List<SysRole> sysRoleList = sysRoleMapper.selectList(null);
        //根据用户id查询中间表获取该用户已经分配的角色，返回SysUserRole对象
        //创建QueryWrapper对象
        QueryWrapper<SysUserRole> sysUserRoleQueryWrapper = new QueryWrapper<>();
        //设置查询条件
        sysUserRoleQueryWrapper.eq("user_id",userId);
        List<SysUserRole> sysUserRoleList = sysUserRoleMapper.selectList(sysUserRoleQueryWrapper);
        //创建一个保存用户已分配角色的id的集合
        List<Long> userRoleIds = new ArrayList<>();
        //遍历用户已分配的角色集合
        for (SysUserRole sysUserRole : sysUserRoleList) {
            //获取sysUserRole对象中的角色id放到userRoleIds中
            userRoleIds.add(sysUserRole.getRoleId());
        }
        //创建返回的Map
        Map<String , Object> returnMap = new HashMap<>();
        //将所有的角色对象放到map中
        returnMap.put("allRoles",sysRoleList);
        //将用户已分配的角色id放到map中
        returnMap.put("userRoleIds",userRoleIds);
        return returnMap;
    }

    @Override
    public void assignRoles(AssginRoleVo assginRoleVo) {
        //获取用户的id
        Long userId = assginRoleVo.getUserId();
        //根据用户id删除中间表中原来分配的角色
        sysUserRoleMapper.delete(new QueryWrapper<SysUserRole>().eq("user_id",userId));
        //获取用户现在分配的角色id
        List<Long> roleIdList = assginRoleVo.getRoleIdList();
        //遍历
        for (Long aLong : roleIdList) {
            //创建SysUserRole对象
            SysUserRole sysUserRole = new SysUserRole();
            //设置用户id
            sysUserRole.setUserId(userId);
            //设置角色id
            sysUserRole.setRoleId(aLong);
            //调用SysUserRoleMapper中插入的方法
            sysUserRoleMapper.insert(sysUserRole);
        }
    }
}
